# -*- coding: utf-8 -*-

import boto3

from ..component import Component


class Aws(Component):

    def _init(self):
        super(Aws, self)._init()

        self.__create_session()

    def get_session(self):
        if not self.__session:
            self.__create_session()

        return self.__session

    def get_resource_by_name(self, name):
        return self.get_session().resource(name)

    def __create_session(self):
        self.__session = boto3.Session(
            aws_access_key_id=self._cfg['aws']['access_key_id'],
            aws_secret_access_key=self._cfg['aws']['secret_access_key'],
            region_name=self._cfg['aws']['region_name']
        )


class AwsServiceResource(Aws):

    def _init(self, sr_name):
        super(AwsServiceResource, self)._init()

        self.__sr_name = sr_name
        self.__create_resource()

    @property
    def resource_name(self):
        return self.__sr_name

    @property
    def resource(self):
        if not self.__resource:
            self.__create_resource()

        return self.__resource

    def __create_resource(self):
        self.__resource = self.get_resource_by_name(self.__sr_name)
